﻿using BCCommon;
using BCCommon.Enums;
using BCData.Common.Case.WebsiteCase;
using BCData.Common.Case.WebsiteCaseAttachment;
using BCDto.Common.Case.WebsiteCase;
using BCDto.Sim.Admin.Admin;
using BCEntity.Common.Case.WebsiteCase;
using BCEntity.Common.Case.WebsiteCaseAttachment;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.Common.Case.WebsiteCase
{
    public class WebsiteCaseService : IWebsiteCaseService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IWebsiteCaseData websiteCaseData;
        private readonly IWebsiteCaseAttachmentData websiteCaseAttachmentData;
        private readonly string key = string.Format("{0}", EntityNameConst.WebsiteCaseEntity);

        public WebsiteCaseService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IWebsiteCaseData websiteCaseData,
            IWebsiteCaseAttachmentData websiteCaseAttachmentData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.websiteCaseData = websiteCaseData;
            this.websiteCaseAttachmentData = websiteCaseAttachmentData;
        }

        #region 检查官网案例是否存在
        public bool Exists(int websiteCaseId)
        {
            return this.websiteCaseData.Exists(websiteCaseId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加官网案例
        public WebsiteCaseDto Add(WebsiteCaseRequestDto requestDto,AdminDto adminDto)
        {
            var entity = requestDto.As<WebsiteCaseEntity>();
            entity.CreateAdminName = adminDto.FullName;
            entity.CreateAdminId = adminDto.AdminId;
            databaseContext.BeginTransaction();
            var result = this.websiteCaseData.Add(entity).Result;
            List<WebsiteCaseAttachmentEntity> list = new List<WebsiteCaseAttachmentEntity>();
            foreach (var item in entity.Attachments)
            {
                item.WebsiteCaseId = result.WebsiteCaseId;
                list.Add(this.websiteCaseAttachmentData.Add(item).Result);
            }
            result.Attachments = list;
            databaseContext.Commit();
            return result.As<WebsiteCaseDto>();
        }
        #endregion

        #region 修改官网案例
        public WebsiteCaseDto Update(WebsiteCasePutDto putDto, AdminDto adminDto)
        {
            if (!this.Exists(putDto.WebsiteCaseId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var entity = putDto.As<WebsiteCaseEntity>();
            entity.EditAdminName = adminDto.FullName;
            entity.EditAdminId = adminDto.AdminId;
            var result = this.websiteCaseData.Update(entity).GetAwaiter().GetResult().As<WebsiteCaseDto>();
            string cacheKey = this.redisService.GetKey(key, entity.WebsiteCaseId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 删除官网案例
        public bool Delete(int websiteCaseId)
        {
            if (!this.Exists(websiteCaseId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.websiteCaseData.Delete(websiteCaseId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteCaseId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 获取官网案例
        public WebsiteCaseDto Get(int websiteCaseId)
        {
            string cacheKey = this.redisService.GetKey(key, websiteCaseId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteCaseData.GetEntity(websiteCaseId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<WebsiteCaseDto>();
        }
        #endregion

        #region 获取官网案例列表
        public IEnumerable<WebsiteCaseViewDto> GetList()
        {
            var result = this.redisService.TryGet(key, () =>
            {
                var caseEntities = this.websiteCaseData.GetList().GetAwaiter().GetResult();
                foreach (var item in caseEntities)
                {
                    item.Attachments = this.websiteCaseAttachmentData.GetList(item.WebsiteCaseId).Result.ToList();
                }
                return caseEntities;
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<WebsiteCaseViewDto>>();
        }
        #endregion

        #region 获取官网案例分页
        public PageableList<WebsiteCaseViewDto> WebsiteQuery(int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteCaseData.Query(true, null, null, "CreateTime", false, pageIndex, pageSize).GetAwaiter().GetResult();
                var caseEntities = data.Item1;
                foreach (var item in caseEntities)
                {
                    item.Attachments = this.websiteCaseAttachmentData.GetList(item.WebsiteCaseId).Result.ToList();
                }

                return new PageableList<WebsiteCaseViewDto>
                {
                    Count = data.Item2,
                    Items = caseEntities.As<IEnumerable<WebsiteCaseViewDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion

        #region 获取官网案例分页
        public PageableList<WebsiteCaseDto> Query(bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, enabled, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteCaseData.Query(enabled, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                var caseEntities = data.Item1;
                foreach(var item in caseEntities)
                {
                    item.Attachments = this.websiteCaseAttachmentData.GetList(item.WebsiteCaseId).Result.ToList();
                }

                return new PageableList<WebsiteCaseDto>
                {
                    Count = data.Item2,
                    Items = caseEntities.As<IEnumerable<WebsiteCaseDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion
        
        #region 启用官网案例
        public bool Enabled(int websiteCaseId)
        {
            if (!this.Exists(websiteCaseId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.websiteCaseData.Enabled(websiteCaseId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteCaseId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 禁用官网案例
        public bool Disabled(int websiteCaseId)
        {
            if (!this.Exists(websiteCaseId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.websiteCaseData.Disabled(websiteCaseId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteCaseId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion
    }
}